package crystal.workshop.ui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Collection;
import java.util.ArrayList;

public class SimpleStructureDataSelectionModel implements
		StructureDataSelectionModel, PropertyChangeListener {
	
	public enum Property {
		SELECTION,
		MODEL
	}
	
	protected transient PropertyChangeSupport propertyChange;
	
	/** The currently selected structures */
	private Collection<StructureDataModel> selection;
	
	
	protected PropertyChangeSupport getPropertyChange() {
		if (propertyChange == null) {
			propertyChange = new PropertyChangeSupport(this);
		};
		return propertyChange;
	}

	@Override
	public void propertyChange(final PropertyChangeEvent arg0) {
		// TODO Auto-generated method stub

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		getPropertyChange().addPropertyChangeListener(listener);
	}

	@Override
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		getPropertyChange().removePropertyChangeListener(listener);
	}

	@Override
	public Collection<StructureDataModel> getSelection() {
		if( selection == null ) {
			selection = new ArrayList<StructureDataModel>(0);
		}
		return selection;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setSelection(Collection<StructureDataModel> selection) {
		Collection<StructureDataModel> oldValue = getSelection();
		this.selection = selection;
		firePropertyChange(Property.SELECTION, oldValue, getSelection());
	}
	
	public void firePropertyChange(final Property property,
			final Object oldValue, final Object newValue) {
		getPropertyChange().firePropertyChange(property.toString(), oldValue, newValue);
	}

}
